import { OptionsCallback } from '@/models/login'
import * as API from '@/service/tool/roomMessageApi'
import { awaitTo } from '@/utils'

interface IState {}

const roomMessage = (options?: OptionsCallback) => {
  const state = reactive<IState>({})

  /** 列表 */
  async function roomStatList(page: number, perPage: number) {
    const { result } = await awaitTo(API.roomStatList({ page, per_page: perPage }))
    return result?.data.data
  }

  /** 聊天记录 */
  async function messagesList(roomStatId: number, page: number, perPage: number) {
    const { result } = await awaitTo(
      API.messagesList({ room_stat_id: roomStatId, page, per_page: perPage }),
    )
    return result?.data.data
  }

  /** 未回复用户列表 */
  async function notReplyUsers(roomStatId: number, page: number, perPage: number) {
    const { result } = await awaitTo(
      API.notReplyUsers({ room_stat_id: roomStatId, page, per_page: perPage }),
    )
    return result?.data.data
  }

  /** 已回复用户列表 */
  async function repliedUsers(roomStatId: number, page: number, perPage: number) {
    const { result } = await awaitTo(
      API.repliedUsers({ room_stat_id: roomStatId, page, per_page: perPage }),
    )
    return result?.data.data
  }

  /** 设置已回复 */
  async function setReplied(roomStatMemberId: number, onSuc: () => void) {
    await uni.showLoading()
    const { result } = await awaitTo(
      API.setReplied({ room_stat_member_id: roomStatMemberId, status: 1 }),
    )
    if (result) {
      onSuc()
    }
    uni.hideLoading()
  }

  return {
    ...toRefs(state),
    roomStatList,
    messagesList,
    notReplyUsers,
    repliedUsers,
    setReplied,
  }
}

export default roomMessage
